﻿#include "CharactersLayer.h"
#include "WaitForConect.h"
#include "Player.h"

#pragma execution_character_set("utf-8")
ChoseCharacterLayer::ChoseCharacterLayer()
{
	size = CCDirector::sharedDirector()->getVisibleSize();
	VisableSize = Director::sharedDirector()->getVisibleSize();
	OrginSize = Director::sharedDirector()->getVisibleOrigin();
	m_TouchedButton = -1;
	IsLoadFinish = false;
	CanEnterGame = false;
	ChoseedCharacterGuid = GUID_ERROR;
	BaseButtonPosX = 0;
	EnableButtonPosX = 0;
	CanTouchButton = true;
	Class = -1;
	IsCharacterLoadFinish = false;
	TopText = nullptr;
	CharacterImage = nullptr;
}

ChoseCharacterLayer::~ChoseCharacterLayer()
{
}

bool ChoseCharacterLayer::init()
{
	bool bRef = true;
	return bRef;
}

void ChoseCharacterLayer::AddCharacterInfo(ChoseCharacterInfo __ChoseCharacterInfo)
{
	bool IsNewOrder = true;

	if (_ChoseCharacterInfo.empty())
		IsNewOrder = true;
	else
	{
		for (int i = 0; i < _ChoseCharacterInfo.size(); i++)
		{
			if (__ChoseCharacterInfo.guid == _ChoseCharacterInfo.at(i).guid)
				IsNewOrder = false;
		}
	}
	if (IsNewOrder)
		_ChoseCharacterInfo.push_back(__ChoseCharacterInfo);
}

void ChoseCharacterLayer::IsLoadingFinish()
{
	IsLoadFinish = true;
	scheduleUpdate();
}

void ChoseCharacterLayer::InitCharacterTableView()
{

	BackGround = Sprite::create("Chose_Character_BackGround.png");
	BackGround->setZOrder(-11);
	BackGround->SetPosition(VisableSize.x / 2, VisableSize.y / 2);
	addChild(BackGround);

	TopTitle = Sprite::create("Chose_Character_Title_Frame.png");
	TopTitle->SetPosition(0 - TopTitle->getBoundingBox().size.width / 2, VisableSize.y - TopTitle->getBoundingBox().size.height);
	TopTitle->runAction(CCMoveTo::create(0.5f, Vec2(OrginSize.x + TopTitle->getBoundingBox().size.width / 2, TopTitle->getPositionY())));
	addChild(TopTitle);

	LeftFrame = Sprite::create("Chose_character_Left_Frame.png");
	LeftFrame->SetPosition(0 - LeftFrame->getBoundingBox().size.width / 2, TopTitle->getBoundingBox().origin.y - VisableSize.y * 0.05 - LeftFrame->getBoundingBox().size.height / 2);
	LeftFrame->runAction(CCMoveTo::create(0.5f, Vec2(OrginSize.x + LeftFrame->getBoundingBox().size.width / 2, LeftFrame->getPositionY())));
	addChild(LeftFrame);

	Left_Power_Title = Sprite::create("Chose_Character_Power_Title.png");
	Left_Power_Title->setPosition(LeftFrame->getContentSize().width / 2, LeftFrame->getContentSize().height - Left_Power_Title->getBoundingBox().size.height);
	LeftFrame->addChild(Left_Power_Title);

	float TotalY = LeftFrame->getBoundingBox().size.height * 0.8f;
	float ProFirstY = TotalY / 8;
	float ProSingleY = TotalY / 4;
	for (int i = PROSS_HP; i != PROSS_SPE + 1; i++)
	{
		CCProgressTimer* TempPro = CCProgressTimer::create(Sprite::create("Chose_Character_Pross_Bar.png"));
		TempPro->setPosition(LeftFrame->getContentSize().width * 0.65f, LeftFrame->getContentSize().height - ProFirstY - (LeftFrame->getBoundingBox().size.height * 0.1f) - ((i - 14) * ProSingleY));
		TempPro->setPercentage(99.0f);
		TempPro->setBarChangeRate(ccp(1, 0));
		TempPro->setMidpoint(ccp(0, 0));
		TempPro->setType(kCCProgressTimerTypeBar);
		TempPro->setTag(i);
		LeftFrame->addChild(TempPro);
		PorgressVector.push_back(TempPro);
		char texturl[255];
		snprintf(texturl, 255, "Chose_Character_Pro_Text_%d.png", i - 14);
		Sprite* Text = Sprite::create(texturl);
		Text->setPosition(LeftFrame->getContentSize().width * 0.25f, TempPro->getPositionY());
		LeftFrame->addChild(Text);
	}

	float FirstY = (VisableSize.y * 0.8f) / 16;
	float SingleY = (VisableSize.y * 0.8f) / 8;
	for (int i = 0; i != 8; i++)
	{
		Sprite* _TempFrame = Sprite::create("Character_Frame.png");
		_TempFrame->SetPosition(VisableSize.x - _TempFrame->getBoundingBox().size.width * 0.53f, VisableSize.y * 0.9f - FirstY - (i * SingleY));
		addChild(_TempFrame);
		_TempFrame->setTag(i);

		std::string name = "";
		uint8 m_class = 0;
		if (i < _ChoseCharacterInfo.size())
		{
			ChoseCharacterInfo _info = _ChoseCharacterInfo.at(i);
			name = _info.name;
			m_class = _info.CharacterClass;
		}
		else
		{
			Create_To_Create_New_Character = Sprite::create("Chose_Character_Click_To_Create.png");
			Create_To_Create_New_Character->setPosition(_TempFrame->getContentSize().width / 2, _TempFrame->getContentSize().height / 2);
			_TempFrame->addChild(Create_To_Create_New_Character);
		}


		LabelTTF* TempName = LabelTTF::create(name.c_str(), "Airal", 14);
		TempName->setPosition(_TempFrame->getContentSize().width * 0.1f + TempName->getBoundingBox().size.width / 2, _TempFrame->getContentSize().height / 2);
		_TempFrame->addChild(TempName);
		BaseButtonPosX = _TempFrame->getPositionX();
		EnableButtonPosX = _TempFrame->getPositionX() - _TempFrame->getBoundingBox().size.width * 0.2f;
	}

	_EnterButton = Sprite::create("ChoseCharacter_Button_Enter.png");
	_EnterButton->SetPosition((VisableSize.x - (VisableSize.x - getChildByTag(1)->getBoundingBox().origin.x)) * 0.7, VisableSize.y * 0.05f + _EnterButton->getContentSize().height / 2);
	addChild(_EnterButton);
	_EnterButton->setTag(ENTER_GAME_BUTTON_TAG);

	EnterButton_Text = Sprite::create("Chose_Character_Enter_Game.png");
	EnterButton_Text->setPosition(_EnterButton->getContentSize().width / 2, _EnterButton->getContentSize().height / 2);
	_EnterButton->addChild(EnterButton_Text);
}

bool ChoseCharacterLayer::onTouchBegan(Touch *touch, Event *unused_event)
{
	if (!CanTouchButton)
		return false;
	int touched = GetSelectedButton(touch->getLocation());
	if (touched == TOUCHED_ERROR)
	{

	}
	else
	{
		m_TouchedButton = touched;
		return true;
	}
	return false;
}

void ChoseCharacterLayer::onTouchMoved(Touch *touch, Event *unused_event){}

void ChoseCharacterLayer::onTouchEnded(Touch *touch, Event *unused_event)
{
	int Touched = GetSelectedButton(touch->getLocation());
	if (Touched == m_TouchedButton && Touched != TOUCHED_ERROR)
	{
		if (Touched >= 0 && Touched <= 7)
		{
			if (Touched >= _ChoseCharacterInfo.size())
			{
				//Do Create Character
			}
			else
			{
				ChoseedCharacterGuid = GUID_ERROR;
				//Do Selected Current Character Actions
				if (Sprite* Button = (Sprite*)getChildByTag(Touched))
				if (Button->getPositionX() >= BaseButtonPosX)
				{
					CanTouchButton = false;
					Sequence* Moving = Sequence::create(CCMoveTo::create(0.5f, Vec2(EnableButtonPosX, Button->getPositionY())), CCCallFunc::create(CC_CALLBACK_0(ChoseCharacterLayer::EnableTouchButton, this)), NULL);
					Button->runAction(Moving);
					IsCharacterLoadFinish = false;
					ChoseedCharacterGuid = _ChoseCharacterInfo.at(m_TouchedButton).guid;
					Class = _ChoseCharacterInfo.at(m_TouchedButton).CharacterClass;
					UpdateEnterGameButtonEnable();
				}
			}
		}
		switch (Touched)
		{
			case ENTER_GAME_BUTTON_TAG:
				//Do Enter Game Or Creat Character
				break;
		}
	}
	ResetButtonPostion(m_TouchedButton);
	m_TouchedButton = GUID_ERROR;
}

int ChoseCharacterLayer::GetSelectedButton(Vec2 Loc)
{
	for (int i = 0; i != END_OF_TAG; i++)
	{
		if (Sprite* Temp = (Sprite*)getChildByTag(i))
		{
			if (Temp->getBoundingBox().containsPoint(Loc))
				return Temp->getTag();
		}
	}

	return TOUCHED_ERROR;
}

void ChoseCharacterLayer::EnableTouchButton()	
{ 
	CanTouchButton = true; 
}

void ChoseCharacterLayer::ResetButtonPostion(uint8 m_touched)
{
	for (int i = 0; i != 8; i++)
	{
		if (i == m_touched)
			continue;
		if (Sprite* Button = (Sprite*)getChildByTag(i))
		{
			if (Button->getPositionX() <= EnableButtonPosX)
			{
				Button->runAction(CCMoveTo::create(0.5f, Vec2(BaseButtonPosX, Button->getPositionY())));
			}
		}
	}
}

void ChoseCharacterLayer::UpdateCharacterDetailsToShow()
{
	char classurl[255];
	snprintf(classurl, 255, "class_name_%d.png", Class);
	if (!TopText)
	{
		TopText = Sprite::create(classurl);
		TopText->setTag(TAG_TOP_TEXT);
		addChild(TopText);
	}
	else
		TopText->setTexture(classurl);

	TopText->SetPosition(TopTitle->getPositionX() + TopText->getBoundingBox().size.width / 2, TopTitle->getPositionY());


	char characterimageurl[255];
	snprintf(characterimageurl, 255, "Character_%d_0.png", Class);
	if (!CharacterImage)
	{
		CharacterImage = Sprite::create(characterimageurl);
		CharacterImage->setTag(TAG_CHARACTER_IMAGE);
		CharacterImage->SetPosition(_EnterButton->getPositionX(), OrginSize.y + CharacterImage->getBoundingBox().size.height * 0.52f);
		CharacterImage->setZOrder(-10);
		addChild(CharacterImage);
	}
	CharacterImage->setTexture(characterimageurl);

}

void ChoseCharacterLayer::UpdateEnterGameButtonEnable()
{
	//格式 操作码 角色guid
	sNotifer->Show("Loading Character Info...");
	sNotifer->setZOrder(getZOrder() + 1);
	char msg[255];
	snprintf(msg, 255, "%d|%d|", CMSG_GET_CHARACTER_INFO, ChoseedCharacterGuid);
	sPlayer->AddMsgToList(msg);
	//需要获取角色信息 异步处理
}

void ChoseCharacterLayer::SetLoadedCharacter(ChosedCharacterDetail _Detail)
{
	_ChosedCharacterDetail.url					= _Detail.url;
	_ChosedCharacterDetail.level				= _Detail.level;
	_ChosedCharacterDetail.base_speed			= _Detail.base_speed;
	_ChosedCharacterDetail.base_heath			= _Detail.base_heath;
	_ChosedCharacterDetail.base_defanc			= _Detail.base_defanc;
	_ChosedCharacterDetail.base_attack			= _Detail.base_attack;
	IsCharacterLoadFinish = true;
	CanEnterGame = true;
}

void ChoseCharacterLayer::UpdateProssBar()
{
	float attpro = _ChosedCharacterDetail.base_attack;
	float defpro = _ChosedCharacterDetail.base_defanc;
	float heapro = _ChosedCharacterDetail.base_heath;
	float spepro = _ChosedCharacterDetail.base_speed;
	for (int i = PROSS_HP; i < PROSS_SPE + 1; i++)
	{
		int changeto = 0;
		if (CCProgressTimer* Temp_Pro = (CCProgressTimer*)LeftFrame->getChildByTag(i))
		{
			switch (i)
			{
			case PROSS_HP:
				changeto = heapro - Temp_Pro->getPercentage();
				break;
			case PROSS_ATT:
				changeto = attpro - Temp_Pro->getPercentage();
				break;
			case PROSS_DEF:
				changeto = defpro - Temp_Pro->getPercentage();
				break;
			case PROSS_SPE:
				changeto = spepro - Temp_Pro->getPercentage();
				break;
			}
			changeto > 0 ? Temp_Pro->setPercentage(Temp_Pro->getPercentage() + 1) : Temp_Pro->setPercentage(Temp_Pro->getPercentage() - 1);
		}
	}
}

void ChoseCharacterLayer::update(float diff)
{
	if (PorgressVector.size() == 4)
		UpdateProssBar();
	if (IsLoadFinish)
	{
		InitCharacterTableView();
		auto listener = EventListenerTouchOneByOne::create();
		listener->setSwallowTouches(true);
		listener->onTouchBegan = CC_CALLBACK_2(ChoseCharacterLayer::onTouchBegan, this);
		listener->onTouchMoved = CC_CALLBACK_2(ChoseCharacterLayer::onTouchMoved, this);
		listener->onTouchEnded = CC_CALLBACK_2(ChoseCharacterLayer::onTouchEnded, this);
		_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
		IsLoadFinish = false;
	}

	if (IsCharacterLoadFinish)
	{
		if (Sprite* EnterGameButton = (Sprite*)getChildByTag(ENTER_GAME_BUTTON_TAG))
		{
			if (ChoseedCharacterGuid >= 0 && CanEnterGame)
				setOpacity(255.0f);
			else
				setOpacity(70.0f);
		}
		IsCharacterLoadFinish = false;
		UpdateCharacterDetailsToShow();
		sNotifer->SetStringDisappearDelay("Load Finished!", 2.0f);
	}
}